﻿using BrnMall.Manager.Domain.AddressBook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.AddressBook
{
    /// <summary>
    /// 客户与标签关系
    /// </summary>
    public class ClientRelationsManager
    {
        static string TableName = "crm_ClientRelations";

        public static bool Insert(ClientRelations model)
        {
            using (var db = new QDMallDatabase())
            {
                 db.Insert(model);
            }
            return model.Id > 0;
        }


        public static int Delete(int Id)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Execute(string.Format("delete from {0} where Id={1}", TableName, Id));
            }
        }

        public static List<ClientRelations> GetList(int staffId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<ClientRelations>(string.Format("select * from {0} where staffId={1}", TableName, staffId));
            }
        }

        public static List<ClientRelations> GetList(int ClientTagId, int page, int pageSize = 10)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<ClientRelations>(page, pageSize, string.Format("select * from {0} where ClientTagId={1}", TableName, ClientTagId));
            }
        }
        public static ClientRelations GetClientRelations(int ClientTagId)
        {
            using (var db = new QDMallDatabase())
            {
                string sql = string.Format("select * from {0} where ClientTagId={1}", TableName, ClientTagId);
                return db.FirstOrDefault<ClientRelations>(sql);
            }
        }


        public static int IsExists(int StaffId, int ClientId, int ClientTagId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.ExecuteScalar<int>(string.Format("select count(1) from {0} where staffId={1} and ClientId={2} and ClientTagId={3}"
                    , TableName, StaffId, ClientId, ClientTagId));
            }
        }


        public static int Add(ClientRelations model)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(model);
            }
            return model.Id;
        }

        public static int Update(int StaffId, int NewStaffId) {
            using (var db = new QDMallDatabase())
            {
                return db.Execute(string.Format("update {0} set StaffId={1} where StaffId={2}",TableName, NewStaffId, StaffId));
            }
        }
    }
}
